package com.ebiz.jo.imageBrowser
{
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.Dictionary;
	
	import org.fas.display.SpringButton;
	import org.fas.tween.FTween;
	import org.fas.utils.FuDisplayer;
	import org.fas.utils.FuMath;
	import org.fas.utils.FuObject;
	
	public class ImageBrowser extends Sprite
	{
		public var MIB:Object;
		public var netCache:NetCache = new NetCache();
		[Embed(source="res_imageBrowser.swf", symbol="CBrowser")]
		private var CBrowser:Class;
		[Embed(source="res_imageBrowser.swf", symbol="CImage")]
		private var CImage:Class;
		[Embed(source="res_imageBrowser.swf", symbol="CLoadingMovie")]
		private var CLoadingMovie:Class;
		
		private var er_sumImage:int = 9;
		private var browser:Sprite;
		private var btnLeft:SimpleButton;
		private var btnRigth:SimpleButton;
		private var coor:Array = new Array();
		private var coorParams:Array = ['x','y','scaleX','scaleY','alpha'];
		private var images:Array = new Array();
		private var tweens:Dictionary = new Dictionary();
		private var loadingMovieAll:Sprite;
		//
		private var currentItemIndex:int = 4;
		//number of all items
		private var numItems:int = 10;
		//number of item every get from service
		private var perNumItems:int = 10;
		//
		public static var instance:ImageBrowser;
		public function ImageBrowser(_mib:Object)
		{
			super();
			instance = this;
			this.MIB = _mib;
			this.netCache.IB = this;
			this.addEventListener(Event.CHANGE,evtChange);
			this.browser = new CBrowser() as Sprite;
			var $browserContainer:Sprite = new Sprite();
			this.addChildAt($browserContainer,0);
			$browserContainer.scrollRect = new Rectangle(0,0,1020,560);
			$browserContainer.x=-500;
			$browserContainer.y=-280;
			this.browser.x = 500;
			browser.y = 280;
			$browserContainer.addChildAt(this.browser,0);
			this.distill();
			//
			this.loadingMovieAll = this.newLoadingMovie()
			this.addChild(this.loadingMovieAll);
		}
		public function showPop($id:Number):void{
			this.MIB.showPop($id);
		}
		private function evtChange(_e:Event):void{
			this.buildClue();
		}
		public function build():void{
			if(this.loadingMovieAll && this.loadingMovieAll.parent==this){
				this.removeChild(this.loadingMovieAll);
			}
			this.buildBtns();
			this.images = new Array();
			for(var i:int=0;i<9;i++){
				this.images[i] = this.createImageC(i);
			}
			this.refreshLv();
		}
		public function destory():void{
			for(var i:int=0;i<this.images.length;i++){
				var ic:ImageContainer = (this.images[i] as ImageContainer);
				this.browser.removeChild(ic.instance);
			}
			this.MIB = null;
			this.images = null;
			this.netCache.destory();
		}
		public function createImageC(i:int):ImageContainer{
			var _imageC:ImageContainer = this.netCache.getItemAt(this.currentItemIndex-4+i);
			FuObject.setTo(_imageC.instance,this.coorParams,this.coor[FuMath.range(i-1,0,6)]);
			this.browser.addChild(_imageC.instance);
			return _imageC;
		}
		public function buildClue():void{
		}
		public function newImage():Sprite{
			var _p:Sprite = new CImage() as Sprite;
			_p.name = FuMath.randomInt(10000).toString();
			return _p;
		}
		public function newLoadingMovie():MovieClip{
			var _m:MovieClip = new this.CLoadingMovie() as MovieClip;
			var _func:Function = function():void{_m.gotoAndStop('loop')};
			_m.addFrameScript(_m.totalFrames-1,_func);
			return _m;
		}
		private function distill():void{
			this.btnLeft = FuDisplayer.distill(this.browser,'ciBtnLeft') as SimpleButton;
			this.addChild(this.btnLeft);
			//
			this.btnRigth = FuDisplayer.distill(this.browser,'ciBtnRight') as SimpleButton;
			this.addChild(this.btnRigth);
			//
			var _coorCi:Sprite;
			for(var i:int=0;i<7;i++){
				_coorCi = FuDisplayer.distill(this.browser,'ciImage'+i) as Sprite;
				this.coor[i] = [_coorCi.x,_coorCi.y,_coorCi.scaleX,_coorCi.scaleY,_coorCi.alpha];
				this.browser.removeChild(_coorCi);
			}
		}
		private function buildBtns():void{
			this.btnLeft.addEventListener(MouseEvent.MOUSE_DOWN,evtBtnClick);
			this.btnRigth.addEventListener(MouseEvent.MOUSE_DOWN,evtBtnClick);
		}
		private function evtBtnClick(_e:MouseEvent):void{
			var _direction:int;//-1:left 1:right
			switch(_e.currentTarget){
				case this.btnLeft:
					_direction = 1;
					break;
				case this.btnRigth:
					_direction = -1;
					break;
			}
			this.move(_direction);
		}
		private function move(_direction:int):void{
			this.currentItemIndex-=_direction;
			if(this.currentItemIndex<0){
				this.currentItemIndex = this.netCache.numItems-1;
			}else if(this.currentItemIndex>=this.netCache.numItems){
				this.currentItemIndex = 0;
			}
			//
			var _ic:ImageContainer;
			var _tween:FTween;
			var _newImages:Array = new Array();
			var _newI:int;
			for(var i:int=0;i<9;i++){
				_ic = this.images[i] as ImageContainer;
				if(_direction==-1 && i==0){//first one
					this.browser.removeChild(_ic.instance);
					_newI = 8;
					_ic = this.createImageC(8);
				}else if(_direction==1 && i==8){//last one
					this.browser.removeChild(_ic.instance);
					_newI = 0;
					_ic = this.createImageC(0);
				}else{
					_newI = i+_direction;
				}
				_newImages[_newI] = _ic;
				if(_newI==0 || _newI==8){
					continue;
				}
				_tween = _ic.tween;
				_tween.fixs(_ic.instance,this.coorParams,this.coor[_newI-1]);
				_tween.start();
			}
			this.images = _newImages;
			this.refreshLv();
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		private function refreshLv():void{
			var _instance:Sprite;
			for(var i:int=0;i<4;i++){
				_instance = (this.images[8-i] as ImageContainer).instance;
				this.browser.addChildAt(_instance,this.browser.numChildren-1);
				_instance = (this.images[i] as ImageContainer).instance;
				this.browser.addChildAt(_instance,this.browser.numChildren-1);
			}
			_instance = (this.images[4] as ImageContainer).instance;
			this.browser.addChildAt(_instance,this.browser.numChildren-1);
		}
	}
}
